

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="lynx">
  <meta name="keywords" content="">
  
    <meta name="description" content="Hexo主要是用于发表博客也就是文章的静态网页框架工具，所以官方很少给到相册搭建的教程。而作为前端小白的我，尝试搭建网页的目的就是为了制作一个网页相册，为此，我死磕了接近一个星期，才大概把这个博客网页集成相册功能的方法搞懂。 Toc    搭建思路 文件夹分配 操作步骤  1.新建相册页面 2.处理图片信息 3.加载js和css文件 4.网页端图片加速   优化  简化create流程 简化oss">
<meta property="og:type" content="article">
<meta property="og:title" content="Hexo基于fluid模板的相册搭建">
<meta property="og:url" content="https://l0223.github.io.git/2024/03/25/Hexo%E5%9F%BA%E4%BA%8Efluid%E4%B8%BB%E9%A2%98%E7%9A%84%E7%9B%B8%E5%86%8C%E6%90%AD%E5%BB%BA/index.html">
<meta property="og:site_name" content="Lynx&#39;s Blog">
<meta property="og:description" content="Hexo主要是用于发表博客也就是文章的静态网页框架工具，所以官方很少给到相册搭建的教程。而作为前端小白的我，尝试搭建网页的目的就是为了制作一个网页相册，为此，我死磕了接近一个星期，才大概把这个博客网页集成相册功能的方法搞懂。 Toc    搭建思路 文件夹分配 操作步骤  1.新建相册页面 2.处理图片信息 3.加载js和css文件 4.网页端图片加速   优化  简化create流程 简化oss">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://picoflynx.oss-cn-guangzhou.aliyuncs.com/source/images/gallery/Sdpaint/00252-642878706-1girl%2Cwhite%20hair%2Cblack%20long%20dress%2C%28flower%20background_1.1%29%2C%28studio%20ghibli_1.2%29%2C.png">
<meta property="article:published_time" content="2024-03-25T22:31:56.000Z">
<meta property="article:modified_time" content="2024-04-08T09:34:59.670Z">
<meta property="article:author" content="Yeahill Lin">
<meta property="article:tag" content="网页搭建">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://picoflynx.oss-cn-guangzhou.aliyuncs.com/source/images/gallery/Sdpaint/00252-642878706-1girl%2Cwhite%20hair%2Cblack%20long%20dress%2C%28flower%20background_1.1%29%2C%28studio%20ghibli_1.2%29%2C.png">
  
  
  
  <title>Hexo基于fluid模板的相册搭建 - Lynx&#39;s Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  



  
<link rel="stylesheet" custom_css="&#x2F;css&#x2F;fold_code,&#x2F;css&#x2F;iconfont_fold&#x2F;iconfont.css" href="/.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"l0223.github.io.git","root":"/","version":"1.9.7","typing":{"enable":true,"typeSpeed":50,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 7.1.1"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Lynx&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/category/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/photos/" target="_self">
                <i class="iconfont icon-image"></i>
                <span>photos</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('https://picoflynx.oss-cn-guangzhou.aliyuncs.com/source/images/gallery/Sdpaint/00252-642878706-1girl%2Cwhite%20hair%2Cblack%20long%20dress%2C%28flower%20background_1.1%29%2C%28studio%20ghibli_1.2%29%2C.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Hexo基于fluid模板的相册搭建"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        lynx
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2024-03-25 22:31" pubdate>
          2024年3月25日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          2.7k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          23 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">Hexo基于fluid模板的相册搭建</h1>
            
            
              <div class="markdown-body">
                
                <p>Hexo主要是用于发表博客也就是文章的静态网页框架工具，所以官方很少给到相册搭建的教程。而作为前端小白的我，尝试搭建网页的目的就是为了制作一个网页相册，为此，我死磕了接近一个星期，才大概把这个博客网页集成相册功能的方法搞懂。</p>
<h1>Toc</h1>
<!-- @import "[TOC]" {cmd="toc" depthFrom=2 depthTo=3 orderedList=false} -->
<!-- code_chunk_output -->
<ul>
<li><a href="#%E6%90%AD%E5%BB%BA%E6%80%9D%E8%B7%AF">搭建思路</a></li>
<li><a href="#%E6%96%87%E4%BB%B6%E5%A4%B9%E5%88%86%E9%85%8D">文件夹分配</a></li>
<li><a href="#%E6%93%8D%E4%BD%9C%E6%AD%A5%E9%AA%A4">操作步骤</a>
<ul>
<li><a href="#1%E6%96%B0%E5%BB%BA%E7%9B%B8%E5%86%8C%E9%A1%B5%E9%9D%A2">1.新建相册页面</a></li>
<li><a href="#2%E5%A4%84%E7%90%86%E5%9B%BE%E7%89%87%E4%BF%A1%E6%81%AF">2.处理图片信息</a></li>
<li><a href="#3%E5%8A%A0%E8%BD%BDjs%E5%92%8Ccss%E6%96%87%E4%BB%B6">3.加载js和css文件</a></li>
<li><a href="#4%E7%BD%91%E9%A1%B5%E7%AB%AF%E5%9B%BE%E7%89%87%E5%8A%A0%E9%80%9F">4.网页端图片加速</a></li>
</ul>
</li>
<li><a href="#%E4%BC%98%E5%8C%96">优化</a>
<ul>
<li><a href="#%E7%AE%80%E5%8C%96create%E6%B5%81%E7%A8%8B">简化create流程</a></li>
<li><a href="#%E7%AE%80%E5%8C%96ossutil%E6%B5%81%E7%A8%8B">简化ossutil流程</a></li>
</ul>
</li>
<li><a href="#%E5%8F%82%E8%80%83">参考</a></li>
</ul>
<!-- /code_chunk_output -->
<h2 id="搭建思路">搭建思路</h2>
<p><img src="https://picoflynx.oss-cn-guangzhou.aliyuncs.com/source/images/gallery/%E6%96%87%E7%AB%A0%E5%9B%BE/202403302122803.png" srcset="/img/loading.gif" lazyload alt="搭建思路"></p>
<h2 id="文件夹分配">文件夹分配</h2>
<p><img src="https://picoflynx.oss-cn-guangzhou.aliyuncs.com/source/images/gallery/%E6%96%87%E7%AB%A0%E5%9B%BE/202403311642349.png" srcset="/img/loading.gif" lazyload alt="文件夹结构"></p>
<ul>
<li>gallery：装载相册原图的文件夹。在该文件夹下，不同主题的相册以<strong>单独</strong>的文件夹存放，并且，相册里面只允许存放<strong>图片格式</strong>的文件</li>
<li>photos：由hexo新建命令生成的页面文件夹，用于构成网页导航中的相册子页面。其中的<strong>index</strong>定义了页面的内容和元数据（通过YAML Front-matter），并会被Hexo转换成最终的静态HTML页面</li>
<li>js：存放source文件下的JavaScript脚本</li>
<li>hexo根目录下的scripts：存放注入代码等需要在根目录执行的JavaScript脚本</li>
</ul>
<h2 id="操作步骤">操作步骤</h2>
<h3 id="1-新建相册页面">1.新建相册页面</h3>
<figure class="highlight plaintext"><i class="iconfont icon-github-fill" type="button" data-toggle="collapse" data-target="#collapse-376hwaluqrb9fe"></i><span>node</span><div class="collapse show" id="collapse-376hwaluqrb9fe"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs node">hexo n page photos<br></code></pre></td></tr></table></div></figure>
<p><a target="_blank" rel="noopener" href="http://xn--index-lz2n363g.md">编辑index.md</a>,输入以下信息形成页面布局</p>
<figure class="highlight markdown"><i class="iconfont icon-github-fill" type="button" data-toggle="collapse" data-target="#collapse-4roj2qluqrb9fe"></i><span>markdown</span><div class="collapse show" id="collapse-4roj2qluqrb9fe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs markdown">---<br>title: Photos<br>date: 2024-03-25 16:57:45<br>subtitle: 岁月留痕<br><span class="hljs-section">layout: photo</span><br><span class="hljs-section">---</span><br><br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span></span><br>.ImageGrid &#123;<br>  width: 100%;<br>  max-width: 1040px;<br>  margin: 0 auto;<br>  text-align: center;<br>&#125;<br>.card &#123;<br>  overflow: hidden;<br>  transition: .3s ease-in-out;<br>  border-radius: 8px;<br>  background-color: #efefef;<br>  padding: 1.4px;<br>&#125;<br>.ImageInCard img &#123;<br>  padding: 0;<br>  border-radius: 8px;<br>  width:100%;<br>  height:100%;<br>&#125;<br>@media (prefers-color-scheme: dark) &#123;<br>  .card &#123;background-color: rgba(180,180,180,0.2)#333;&#125;<br>&#125;<br><br><span class="language-xml"><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span></span><br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;imageTab&quot;</span>&gt;</span></span><span class="language-xml"><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span><br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;ImageGrid&quot;</span>&gt;</span></span><span class="language-xml"><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span><br></code></pre></td></tr></table></div></figure>
<h3 id="2-处理图片信息">2.处理图片信息</h3>
<ol>
<li>
<p>创建存放照片原图文件夹images/gallery，子目录为根据主题分类的相册，如“广州”、“上海”…</p>
</li>
<li>
<p>在gallery文件夹下新建js文件&quot;create.js&quot;，用于处理图片信息</p>
<figure class="highlight js"><i class="iconfont icon-github-fill" type="button" data-toggle="collapse" data-target="#collapse-bikcv1luqrb9fe"></i><span>js</span><div class="collapse show" id="collapse-bikcv1luqrb9fe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><code class="hljs js"># create.<span class="hljs-property">js</span><br><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;fs-extra&#x27;</span>);<br><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;path&#x27;</span>);<br><span class="hljs-keyword">const</span> imageSize = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;image-size&#x27;</span>);  # 这里没有安装 image-size，需要npm安装下:   npm i -S image-size<br><br><span class="hljs-keyword">const</span> rootPath=<span class="hljs-string">&quot;gallery/&quot;</span>   <span class="hljs-comment">//相册相对路径，也可填绝对路径，指向相册的目录，目的为了生成json</span><br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">PhotoExtension</span> &#123;<br>    <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) &#123;<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">size</span> = <span class="hljs-number">64</span>;<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">offset</span> = [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>];<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Photo</span> &#123;<br>    <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) &#123;<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">dirName</span> = <span class="hljs-string">&#x27;&#x27;</span>;<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">fileName</span> = <span class="hljs-string">&#x27;&#x27;</span>;<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">iconID</span> = <span class="hljs-string">&#x27;&#x27;</span>;<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">extension</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PhotoExtension</span>();<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">PhotoGroup</span> &#123;<br>    <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) &#123;<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = <span class="hljs-string">&#x27;&#x27;</span>;<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">children</span> = [];<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">createPlotIconsData</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-keyword">let</span> allPlots = [];<br>    <span class="hljs-keyword">let</span> allPlotGroups = [];<br><br>    <span class="hljs-keyword">const</span> plotJsonFile = path.<span class="hljs-title function_">join</span>(__dirname, <span class="hljs-string">&#x27;./photosInfo.json&#x27;</span>);<br>    <span class="hljs-keyword">const</span> plotGroupJsonFile = path.<span class="hljs-title function_">join</span>(__dirname, <span class="hljs-string">&#x27;./photos.json&#x27;</span>);<br><br>    <span class="hljs-keyword">if</span> (fs.<span class="hljs-title function_">existsSync</span>(plotJsonFile)) &#123;<br>        allPlots = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(fs.<span class="hljs-title function_">readFileSync</span>(plotJsonFile));<br>    &#125;<br><br>    <span class="hljs-keyword">if</span> (fs.<span class="hljs-title function_">existsSync</span>(plotGroupJsonFile)) &#123;<br>        allPlotGroups = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(fs.<span class="hljs-title function_">readFileSync</span>(plotGroupJsonFile));<br>    &#125;<br><br>    fs.<span class="hljs-title function_">readdirSync</span>(__dirname).<span class="hljs-title function_">forEach</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params">dirName</span>) &#123;<br>        <span class="hljs-keyword">const</span> stats = fs.<span class="hljs-title function_">statSync</span>(path.<span class="hljs-title function_">join</span>(__dirname, dirName));<br>        <span class="hljs-keyword">const</span> isDir = stats.<span class="hljs-title function_">isDirectory</span>();<br>        <span class="hljs-keyword">if</span> (isDir) &#123;<br>            <span class="hljs-keyword">const</span> subfiles = fs.<span class="hljs-title function_">readdirSync</span>(path.<span class="hljs-title function_">join</span>(__dirname, dirName));<br>            subfiles.<span class="hljs-title function_">forEach</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params">subfileName</span>) &#123;<br>                <span class="hljs-comment">// 如果已经存在 则不再处理</span><br>                <span class="hljs-comment">// if (allPlots.find(o =&gt; o.fileName === subfileName &amp;&amp; o.dirName === dirName)) &#123;</span><br>                <span class="hljs-comment">//     return;</span><br>                <span class="hljs-comment">// &#125;</span><br><br>                <span class="hljs-comment">// 新增标</span><br>                <span class="hljs-keyword">const</span> plot = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Photo</span>();<br>                plot.<span class="hljs-property">dirName</span> = dirName;<br>                plot.<span class="hljs-property">fileName</span> = subfileName;<br>                <span class="hljs-keyword">const</span> imageInfo = <span class="hljs-title function_">imageSize</span>(rootPath+dirName + <span class="hljs-string">&quot;/&quot;</span> + subfileName);<br>                plot.<span class="hljs-property">iconID</span> = imageInfo.<span class="hljs-property">width</span> + <span class="hljs-string">&#x27;.&#x27;</span> + imageInfo.<span class="hljs-property">height</span> + <span class="hljs-string">&#x27; &#x27;</span> + subfileName;<br>                allPlots.<span class="hljs-title function_">push</span>(plot);<br>                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`RD: createPlotIconsData -&gt; new plot`</span>, plot);<br><br>                <span class="hljs-comment">// 为新增标添加分组 暂时以它所处的文件夹为分组</span><br>                <span class="hljs-keyword">let</span> group = allPlotGroups.<span class="hljs-title function_">find</span>(<span class="hljs-function"><span class="hljs-params">o</span> =&gt;</span> o.<span class="hljs-property">name</span> === dirName);<br>                <span class="hljs-keyword">if</span> (!group) &#123;<br>                    group = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PhotoGroup</span>();<br>                    group.<span class="hljs-property">name</span> = dirName;<br>                    allPlotGroups.<span class="hljs-title function_">push</span>(group);<br>                    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`RD: createPlotIconsData -&gt; new group`</span>, group);<br>                &#125;<br>                group.<span class="hljs-property">children</span>.<span class="hljs-title function_">push</span>(plot.<span class="hljs-property">iconID</span>);<br>            &#125;);<br>        &#125;<br>    &#125;);<br><br>    fs.<span class="hljs-title function_">writeJSONSync</span>(plotJsonFile, allPlots);<br>    fs.<span class="hljs-title function_">writeJSONSync</span>(plotGroupJsonFile, allPlotGroups);<br>&#125;<br><br><span class="hljs-title function_">createPlotIconsData</span>();<br></code></pre></td></tr></table></div></figure>
</li>
<li>
<p><strong>执行create.js文件</strong>，在gallery文件夹下生成包含图片信息的JSON文件（photosInfo.json和photos.json`）</p>
<figure class="highlight cmd"><i class="iconfont icon-github-fill" type="button" data-toggle="collapse" data-target="#collapse-zu5oz0luqrb9fe"></i><span>cmd</span><div class="collapse show" id="collapse-zu5oz0luqrb9fe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs cmd"># 相册的生成需要先用create.js脚本对images\gallery\下的相册文件进行预处理<br># 示例 F:myblog\source\images&gt;node gallery/create.js<br>node gallery/create.js<br></code></pre></td></tr></table></div></figure>
</li>
<li>
<p>将photos.json<strong>拷贝</strong>到source目录下的photos页面文件夹中</p>
</li>
</ol>
<h3 id="3-加载js和css文件">3.加载js和css文件</h3>
<ol>
<li>
<p>在 /source/js/ 目录下创建 photoWall.js，用于在网页上动态创建和管理图片相册的界面</p>
<figure class="highlight js"><i class="iconfont icon-github-fill" type="button" data-toggle="collapse" data-target="#collapse-9ignz0luqrb9fe"></i><span>js</span><div class="collapse show" id="collapse-9ignz0luqrb9fe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> imgDataPath = <span class="hljs-string">&quot;/photos/photos.json&quot;</span>; <span class="hljs-comment">//图片名称高宽信息json文件路径</span><br><span class="hljs-keyword">var</span> imgPath = <span class="hljs-string">&quot;/sources/images/gallery/&quot;</span>; <span class="hljs-comment">//图片访问路径, 这里示例是本地路径，后续会提供网页端图片加载加速方案</span><br><span class="hljs-keyword">var</span> imgMaxNum = <span class="hljs-number">50</span>; <span class="hljs-comment">//图片显示数量</span><br><br><span class="hljs-keyword">var</span> windowWidth =<br>  <span class="hljs-variable language_">window</span>.<span class="hljs-property">innerWidth</span> ||<br>  <span class="hljs-variable language_">document</span>.<span class="hljs-property">documentElement</span>.<span class="hljs-property">clientWidth</span> ||<br>  <span class="hljs-variable language_">document</span>.<span class="hljs-property">body</span>.<span class="hljs-property">clientWidth</span>;<br><span class="hljs-keyword">if</span> (windowWidth &lt; <span class="hljs-number">768</span>) &#123;<br>  <span class="hljs-keyword">var</span> imageWidth = <span class="hljs-number">145</span>; <span class="hljs-comment">//图片显示宽度(手机端)</span><br>&#125; <span class="hljs-keyword">else</span> &#123;<br>  <span class="hljs-keyword">var</span> imageWidth = <span class="hljs-number">250</span>; <span class="hljs-comment">//图片显示宽度</span><br>&#125;<br><br><span class="hljs-keyword">const</span> photo = &#123;<br>  <span class="hljs-attr">page</span>: <span class="hljs-number">1</span>,<br>  <span class="hljs-attr">offset</span>: imgMaxNum,<br>  <span class="hljs-attr">init</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-keyword">var</span> that = <span class="hljs-variable language_">this</span>;<br>    $.<span class="hljs-title function_">getJSON</span>(imgDataPath, <span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) &#123;<br>      that.<span class="hljs-title function_">render</span>(that.<span class="hljs-property">page</span>, data);<br>      <span class="hljs-comment">//that.scroll(data);</span><br>      that.<span class="hljs-title function_">eventListen</span>(data);<br>    &#125;);<br>  &#125;,<br>  <span class="hljs-title function_">constructHtml</span>(<span class="hljs-params">options</span>) &#123;<br>    <span class="hljs-keyword">const</span> &#123;<br>      imageWidth,<br>      imageX,<br>      imageY,<br>      name,<br>      imgPath,<br>      imgName,<br>      imgNameWithPattern,<br>    &#125; = options;<br>    <span class="hljs-keyword">const</span> htmlEle = <span class="hljs-string">`&lt;div class=&quot;card lozad&quot; style=&quot;width:<span class="hljs-subst">$&#123;imageWidth&#125;</span>px&quot;&gt;</span><br><span class="hljs-string">                  &lt;div class=&quot;ImageInCard&quot; style=&quot;height:<span class="hljs-subst">$&#123;</span></span><br><span class="hljs-subst"><span class="hljs-string">                    (imageWidth * imageY) / imageX</span></span><br><span class="hljs-subst"><span class="hljs-string">                  &#125;</span>px&quot;&gt;</span><br><span class="hljs-string">                    &lt;a data-fancybox=&quot;gallery&quot; href=&quot;<span class="hljs-subst">$&#123;imgPath&#125;</span><span class="hljs-subst">$&#123;name&#125;</span>/<span class="hljs-subst">$&#123;imgNameWithPattern&#125;</span>&quot;</span><br><span class="hljs-string">                          data-caption=&quot;<span class="hljs-subst">$&#123;imgName&#125;</span>&quot; title=&quot;<span class="hljs-subst">$&#123;imgName&#125;</span>&quot;&gt;</span><br><span class="hljs-string">                            &lt;img  class=&quot;lazyload&quot; data-src=&quot;<span class="hljs-subst">$&#123;imgPath&#125;</span><span class="hljs-subst">$&#123;name&#125;</span>/<span class="hljs-subst">$&#123;imgNameWithPattern&#125;</span>&quot;</span><br><span class="hljs-string">                            src=&quot;&quot;</span><br><span class="hljs-string">                            onload=&quot;lzld(this)&quot;</span><br><span class="hljs-string">                            lazyload=&quot;auto&quot;&gt;</span><br><span class="hljs-string">                        &lt;/a&gt;</span><br><span class="hljs-string">                  &lt;/div&gt;</span><br><span class="hljs-string">                &lt;/div&gt;`</span>;<br>    <span class="hljs-keyword">return</span> htmlEle;<br>  &#125;,<br>  <span class="hljs-attr">render</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">page, data = []</span>) &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">data</span> = data;<br>    <span class="hljs-keyword">if</span> (!data.<span class="hljs-property">length</span>) <span class="hljs-keyword">return</span>;<br>    <span class="hljs-keyword">var</span> html,<br>      imgNameWithPattern,<br>      imgName,<br>      imageSize,<br>      imageX,<br>      imageY,<br>      li = <span class="hljs-string">&quot;&quot;</span>;<br><br>    <span class="hljs-keyword">let</span> liHtml = <span class="hljs-string">&quot;&quot;</span>;<br>    <span class="hljs-keyword">let</span> contentHtml = <span class="hljs-string">&quot;&quot;</span>;<br><br>    data.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">item, index</span>) =&gt;</span> &#123;<br>      <span class="hljs-keyword">const</span> activeClass = index === <span class="hljs-number">0</span> ? <span class="hljs-string">&quot;active&quot;</span> : <span class="hljs-string">&quot;&quot;</span>;<br>      liHtml += <span class="hljs-string">`&lt;li class=&quot;nav-item&quot; role=&quot;presentation&quot;&gt;</span><br><span class="hljs-string">          &lt;a class=&quot;nav-link <span class="hljs-subst">$&#123;activeClass&#125;</span> photo-tab&quot; id=&quot;home-tab&quot; photo-uuid=&quot;<span class="hljs-subst">$&#123;item.name&#125;</span>&quot; data-toggle=&quot;tab&quot; href=&quot;#<span class="hljs-subst">$&#123;item.name&#125;</span>&quot;  role=&quot;tab&quot; aria-controls=&quot;<span class="hljs-subst">$&#123;item.name&#125;</span>&quot; aria-selected=&quot;true&quot;&gt;<span class="hljs-subst">$&#123;item.name&#125;</span>&lt;/a&gt;</span><br><span class="hljs-string">        &lt;/li&gt;`</span>;<br>    &#125;);<br>    <span class="hljs-keyword">const</span> [initData = &#123;&#125;] = data;<br>    <span class="hljs-keyword">const</span> &#123; children = [],name &#125; = initData;<br>    children.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">item, index</span>) =&gt;</span> &#123;<br>      imgNameWithPattern = item.<span class="hljs-title function_">split</span>(<span class="hljs-string">&quot; &quot;</span>)[<span class="hljs-number">1</span>];<br>      imgName = imgNameWithPattern.<span class="hljs-title function_">split</span>(<span class="hljs-string">&quot;.&quot;</span>)[<span class="hljs-number">0</span>];<br>      imageSize = item.<span class="hljs-title function_">split</span>(<span class="hljs-string">&quot; &quot;</span>)[<span class="hljs-number">0</span>];<br>      imageX = imageSize.<span class="hljs-title function_">split</span>(<span class="hljs-string">&quot;.&quot;</span>)[<span class="hljs-number">0</span>];<br>      imageY = imageSize.<span class="hljs-title function_">split</span>(<span class="hljs-string">&quot;.&quot;</span>)[<span class="hljs-number">1</span>];<br>      <span class="hljs-keyword">let</span> imgOptions = &#123;<br>        imageWidth,<br>        imageX,<br>        imageY,<br>        name,<br>        imgName,<br>        imgPath,<br>        imgNameWithPattern,<br>      &#125;;<br>      li += <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">constructHtml</span>(imgOptions);<br>    &#125;);<br>    contentHtml += <span class="hljs-string">` &lt;div class=&quot;tab-pane fade show active&quot;  role=&quot;tabpanel&quot; aria-labelledby=&quot;home-tab&quot;&gt;<span class="hljs-subst">$&#123;li&#125;</span>&lt;/div&gt;`</span>;<br><br>    <span class="hljs-keyword">const</span> ulHtml = <span class="hljs-string">`&lt;ul class=&quot;nav nav-tabs&quot; id=&quot;myTab&quot; role=&quot;tablist&quot;&gt;<span class="hljs-subst">$&#123;liHtml&#125;</span>&lt;/ul&gt;`</span>;<br>    <span class="hljs-keyword">const</span> tabContent = <span class="hljs-string">`&lt;div class=&quot;tab-content&quot; id=&quot;myTabContent&quot;&gt;<span class="hljs-subst">$&#123;contentHtml&#125;</span>&lt;/div&gt;`</span>;<br><br>    $(<span class="hljs-string">&quot;#imageTab&quot;</span>).<span class="hljs-title function_">append</span>(ulHtml);<br>    $(<span class="hljs-string">&quot;.ImageGrid&quot;</span>).<span class="hljs-title function_">append</span>(tabContent);<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">minigrid</span>();<br>  &#125;,<br>  <span class="hljs-attr">eventListen</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) &#123;<br>    <span class="hljs-keyword">let</span> self = <span class="hljs-variable language_">this</span>;<br>    <span class="hljs-keyword">var</span> html,<br>      imgNameWithPattern,<br>      imgName,<br>      imageSize,<br>      imageX,<br>      imageY,<br>      li = <span class="hljs-string">&quot;&quot;</span>;<br>    $(<span class="hljs-string">&#x27;a[data-toggle=&quot;tab&quot;]&#x27;</span>).<span class="hljs-title function_">on</span>(<span class="hljs-string">&quot;shown.bs.tab&quot;</span>, <span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) &#123;<br>      $(<span class="hljs-string">&quot;.ImageGrid&quot;</span>).<span class="hljs-title function_">empty</span>();<br>      <span class="hljs-keyword">const</span> selectId = $(e.<span class="hljs-property">target</span>).<span class="hljs-title function_">attr</span>(<span class="hljs-string">&quot;photo-uuid&quot;</span>);<br>      <span class="hljs-keyword">const</span> selectedData = data.<span class="hljs-title function_">find</span>(<span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> data.<span class="hljs-property">name</span> === selectId) || &#123;&#125;;<br>      <span class="hljs-keyword">const</span> &#123; children,name &#125; = selectedData;<br>      <span class="hljs-keyword">let</span> li = <span class="hljs-string">&quot;&quot;</span>;<br>      children.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">item, index</span>) =&gt;</span> &#123;<br>        imgNameWithPattern = item.<span class="hljs-title function_">split</span>(<span class="hljs-string">&quot; &quot;</span>)[<span class="hljs-number">1</span>];<br>        imgName = imgNameWithPattern.<span class="hljs-title function_">split</span>(<span class="hljs-string">&quot;.&quot;</span>)[<span class="hljs-number">0</span>];<br>        imageSize = item.<span class="hljs-title function_">split</span>(<span class="hljs-string">&quot; &quot;</span>)[<span class="hljs-number">0</span>];<br>        imageX = imageSize.<span class="hljs-title function_">split</span>(<span class="hljs-string">&quot;.&quot;</span>)[<span class="hljs-number">0</span>];<br>        imageY = imageSize.<span class="hljs-title function_">split</span>(<span class="hljs-string">&quot;.&quot;</span>)[<span class="hljs-number">1</span>];<br>        <span class="hljs-keyword">let</span> imgOptions = &#123;<br>          imageWidth,<br>          imageX,<br>          imageY,<br>          name,<br>          imgName,<br>          imgPath,<br>          imgNameWithPattern,<br>        &#125;;<br>        li += self.<span class="hljs-title function_">constructHtml</span>(imgOptions);<br>      &#125;);<br>      $(<span class="hljs-string">&quot;.ImageGrid&quot;</span>).<span class="hljs-title function_">append</span>(li);<br>      self.<span class="hljs-title function_">minigrid</span>();<br>    &#125;);<br>  &#125;,<br>  <span class="hljs-attr">minigrid</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-keyword">var</span> grid = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Minigrid</span>(&#123;<br>      <span class="hljs-attr">container</span>: <span class="hljs-string">&quot;.ImageGrid&quot;</span>,<br>      <span class="hljs-attr">item</span>: <span class="hljs-string">&quot;.card&quot;</span>,<br>      <span class="hljs-attr">gutter</span>: <span class="hljs-number">12</span>,<br>    &#125;);<br>    grid.<span class="hljs-title function_">mount</span>();<br>    $(<span class="hljs-variable language_">window</span>).<span class="hljs-title function_">resize</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123;<br>      grid.<span class="hljs-title function_">mount</span>();<br>    &#125;);<br>  &#125;,<br>&#125;;<br>photo.<span class="hljs-title function_">init</span>();<br></code></pre></td></tr></table></div></figure>
</li>
<li>
<p>使用注册器将需要的js,css注入,在/scripts/injector.js(如没有,则创建)中输入以下内容:</p>
<figure class="highlight js"><i class="iconfont icon-github-fill" type="button" data-toggle="collapse" data-target="#collapse-ehz7ylluqrb9fe"></i><span>js</span><div class="collapse show" id="collapse-ehz7ylluqrb9fe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> &#123; <span class="hljs-attr">root</span>: siteRoot = <span class="hljs-string">&quot;/&quot;</span> &#125; = hexo.<span class="hljs-property">config</span>;<br><span class="hljs-comment">// layout为photo的时候导入这些js与css</span><br>hexo.<span class="hljs-property">extend</span>.<span class="hljs-property">injector</span>.<span class="hljs-title function_">register</span>(<br>  <span class="hljs-string">&quot;body_end&quot;</span>,<br>  <span class="hljs-string">`</span><br><span class="hljs-string">  &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css&quot;&gt;</span><br><span class="hljs-string">  &lt;script src=&quot;//cdn.jsdelivr.net/npm/minigrid@3.1.1/dist/minigrid.min.js&quot;&gt;&lt;/script&gt;</span><br><span class="hljs-string">  &lt;script src=&quot;https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js&quot;&gt;&lt;/script&gt;</span><br><span class="hljs-string"> &lt;script src=&quot;https://cdn.bootcdn.net/ajax/libs/lazyloadjs/3.2.2/lazyload.js&quot;&gt;&lt;/script&gt;</span><br><span class="hljs-string">    &lt;script defer src=&quot;<span class="hljs-subst">$&#123;siteRoot&#125;</span>js/photoWall.js&quot;&gt;&lt;/script&gt;`</span>,<br>  <span class="hljs-string">&quot;photo&quot;</span><br>);<br></code></pre></td></tr></table></div></figure>
</li>
</ol>
<h3 id="4-网页端图片加速">4.网页端图片加速</h3>
<p>​	因为我是基于githubpage部署的博客，国内访问的时候，图片文件加载起来巨慢从而导致导致网页基本打不开，刚开始我参考<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/346643522">如何使用jsDelivr+Github 实现免费CDN加速?</a>搭建了一个github仓库用于存放相册并用于cdn加速，但实际效果还是不行，因此，我想到了用阿里云oss图床，提高网页中图片加载速度，从而提高网页加载速度。</p>
<ol>
<li>
<p>文章中图片：采用typora+picgo+阿里云oss方案，参考链接：<a target="_blank" rel="noopener" href="https://www.cnblogs.com/myworld7/p/13132549.html">【Typora】typora+picgo+阿里云oss搭建图床</a>。</p>
</li>
<li>
<p>相册图片：阿里云ossutil工具，具体方法参考官方说明：<a target="_blank" rel="noopener" href="https://help.aliyun.com/zh/oss/developer-reference/overview-59?spm=5176.8465980.help.dexternal.9c691450trHl8S">ossutil概述</a>。</p>
<ul>
<li>如果使用该方案photowall.js中的图片访问路径（imgPath）需更改为阿里云文件夹中的前缀。如“<a target="_blank" rel="noopener" href="https://picoflynx.oss-cn-XXX.aliyuncs.com/source/images/gallery/%E2%80%9D">https://picoflynx.oss-cn-XXX.aliyuncs.com/source/images/gallery/”</a></li>
<li>为了实现相对于photowall.js图片访问路径与本地相册路径的一致性，我把整个source文件都上传至oss了</li>
</ul>
<p>至此，一个基于hexo的fluid主题的相册就搭建完成啦~，效果如图：</p>
<p><img src="https://picoflynx.oss-cn-guangzhou.aliyuncs.com/source/images/gallery/%E6%96%87%E7%AB%A0%E5%9B%BE/202403302209080.png" srcset="/img/loading.gif" lazyload alt="相册效果图"></p>
</li>
</ol>
<h2 id="优化">优化</h2>
<h3 id="简化create流程">简化create流程</h3>
<p>​	每次上传图片的时候，都需要删除photos.json，再重新执行create.js生成新的photos.json，然后再复制到photos页面文件夹…太麻烦了</p>
<p><strong>解决方案</strong>：写bat脚本执行这个过程（本人技术有限，只想到了这个…）</p>
<ul>
<li>
<p>先写一个批处理文件（run-node-app.bat)置于image文件夹，用于删除旧photos.json,并执行create.js</p>
<figure class="highlight bat"><i class="iconfont icon-github-fill" type="button" data-toggle="collapse" data-target="#collapse-yyh9xeluqrb9fe"></i><span>bat</span><div class="collapse show" id="collapse-yyh9xeluqrb9fe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs bat">@<span class="hljs-built_in">echo</span> off<br><span class="hljs-built_in">cd</span> /d &quot;F:myblog\source\images&quot;<br><br>:: 定义要删除的子文件夹中的文件路径<br><span class="hljs-built_in">SET</span> &quot;FILE_PATH_1=gallery\photos.json&quot;<br><span class="hljs-built_in">SET</span> &quot;FILE_PATH_2=gallery\photosInfo.json&quot;<br><br>:: 删除第一个文件<br><span class="hljs-built_in">del</span> &quot;<span class="hljs-variable">%FILE_PATH_1%</span>&quot; <span class="hljs-number">2</span>&gt;<span class="hljs-built_in">nul</span><br><span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exist</span> &quot;<span class="hljs-variable">%FILE_PATH_1%</span>&quot; (<br>    <span class="hljs-built_in">echo</span> The file <span class="hljs-variable">%FILE_PATH_1%</span> has been deleted successfully.<br>) <span class="hljs-keyword">else</span> (<br>    <span class="hljs-built_in">echo</span> The file <span class="hljs-variable">%FILE_PATH_1%</span> was <span class="hljs-keyword">not</span> found or could <span class="hljs-keyword">not</span> be deleted.<br>)<br><br>:: 删除第二个文件<br><span class="hljs-built_in">del</span> &quot;<span class="hljs-variable">%FILE_PATH_2%</span>&quot; <span class="hljs-number">2</span>&gt;<span class="hljs-built_in">nul</span><br><span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exist</span> &quot;<span class="hljs-variable">%FILE_PATH_2%</span>&quot; (<br>    <span class="hljs-built_in">echo</span> The file <span class="hljs-variable">%FILE_PATH_2%</span> has been deleted successfully.<br>) <span class="hljs-keyword">else</span> (<br>    <span class="hljs-built_in">echo</span> The file <span class="hljs-variable">%FILE_PATH_2%</span> was <span class="hljs-keyword">not</span> found or could <span class="hljs-keyword">not</span> be deleted.<br>)<br><br>:: 执行create.js<br>node &quot;gallery\create.js&quot;<br><br><span class="hljs-built_in">pause</span><br></code></pre></td></tr></table></div></figure>
</li>
<li>
<p>在source文件夹下写一个批处理文件（img-refresh.bat)，用于调用run-node-app.bat,并覆盖photos/photos.js</p>
<figure class="highlight bat"><i class="iconfont icon-github-fill" type="button" data-toggle="collapse" data-target="#collapse-7b9mykluqrb9fe"></i><span>bat</span><div class="collapse show" id="collapse-7b9mykluqrb9fe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs bat">@<span class="hljs-built_in">echo</span> off<br>:: 处理相册gallery<br><span class="hljs-built_in">cd</span> /d &quot;F:myblog\source\images&quot;<br><span class="hljs-keyword">call</span> &quot;run-node-app.bat&quot;<br><span class="hljs-built_in">echo</span> &quot;run-node-app.bat has been executed.&quot;<br><br><span class="hljs-built_in">pause</span><br><br>::复制粘贴至photos<br><br><span class="hljs-built_in">cd</span> /d &quot;F:myblog\source&quot;<br><br>:: 定义要删除的photos中的文件路径<br><span class="hljs-built_in">SET</span> &quot;FILE_PATH_1=photos\photos.json&quot;<br><br>:: 定义要复制的文件路径和目标路径<br><span class="hljs-built_in">SET</span> &quot;SOURCE_FILE_PATH=images\gallery\photos.json&quot;<br><span class="hljs-built_in">SET</span> &quot;TARGET_FILE_PATH=photos\photos.json&quot;<br><br>:: 删除子文件夹photos中的一个文件<br><span class="hljs-built_in">del</span> &quot;<span class="hljs-variable">%FILE_PATH_1%</span>&quot; <span class="hljs-number">2</span>&gt;<span class="hljs-built_in">nul</span><br><span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exist</span> &quot;<span class="hljs-variable">%FILE_PATH_1%</span>&quot; (<br>    <span class="hljs-built_in">echo</span> The file <span class="hljs-variable">%FILE_PATH_1%</span> has been deleted successfully.<br>) <span class="hljs-keyword">else</span> (<br>    <span class="hljs-built_in">echo</span> The file <span class="hljs-variable">%FILE_PATH_1%</span> was <span class="hljs-keyword">not</span> found or could <span class="hljs-keyword">not</span> be deleted.<br>)<br><br>:: 将images/gallery/中的文件复制到photos中<br><span class="hljs-built_in">copy</span> &quot;<span class="hljs-variable">%SOURCE_FILE_PATH%</span>&quot; &quot;<span class="hljs-variable">%TARGET_FILE_PATH%</span>&quot; /y<br><span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exist</span> &quot;<span class="hljs-variable">%TARGET_FILE_PATH%</span>&quot; (<br>    <span class="hljs-built_in">echo</span> Failed to <span class="hljs-built_in">copy</span> the file from subfolder1 to subfolder2.<br>    <span class="hljs-keyword">goto</span> End<br>) <span class="hljs-keyword">else</span> (<br>    <span class="hljs-built_in">echo</span> The file has been copied successfully from subfolder1 to subfolder2.<br>)<br><br><span class="hljs-built_in">pause</span><br></code></pre></td></tr></table></div></figure>
</li>
<li>
<p>该法方法脚本编写过程可能有点低级，但最终实现的效果是，只需要在相册更新之后，双击一下img-refresh.bat就能实现相册更新了</p>
</li>
</ul>
<h3 id="简化ossutil流程">简化ossutil流程</h3>
<p><strong>痛点</strong>：ossutil命令太难用了，搞了好久都没明白怎么实现命令简化</p>
<p><strong>解决方案</strong>：那就又bat吧。编写一个bat(osssync.bat)置于image文件夹下，用于一键同步本地相册于oss</p>
<figure class="highlight bat"><i class="iconfont icon-github-fill" type="button" data-toggle="collapse" data-target="#collapse-hupklvluqrb9fe"></i><span>bat</span><div class="collapse show" id="collapse-hupklvluqrb9fe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs bat">@<span class="hljs-built_in">echo</span> off<br><br>ossutil64 sync F:myblog\source\images oss://bucket名/source/images/ --force<br>:: --force为可选项，当使用ossutil64 sync命令同步文件夹到阿里云OSS时，确实会提示你确认每个文件的上传操作。为了简化这个过程，避免每次上传时都需要手动输入确认（y/n），在命令行中添加--force选项。让ossutil在同步时自动覆盖目标OSS上的文件，而不需要进一步的确认。<br><br><span class="hljs-built_in">pause</span><br></code></pre></td></tr></table></div></figure>
<h2 id="参考">参考</h2>
<ol>
<li>
<p><a target="_blank" rel="noopener" href="https://zyxelva.github.io/posts/45280.html#3-%E5%8A%A0%E8%BD%BD-js%E5%92%8Ccss%E6%96%87%E4%BB%B6">【Hexo】基于Fluid主题的Hexo增加相册功能实践 | Kezade</a></p>
</li>
<li>
<p><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/346643522">如何使用jsDelivr+Github 实现免费CDN加速? - 知乎</a></p>
</li>
<li>
<p><a target="_blank" rel="noopener" href="https://zyxelva.github.io/posts/45280.html#3-%E5%8A%A0%E8%BD%BD-js%E5%92%8Ccss%E6%96%87%E4%BB%B6">【Hexo】基于Fluid主题的Hexo增加相册功能实践</a></p>
</li>
</ol>
<link rel="stylesheet" href="/css/spoiler.css" type="text/css"><script src="/js/spoiler.js" type="text/javascript" async></script>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E7%94%9F%E4%BA%A7%E6%8C%87%E5%8D%97/" class="category-chain-item">生产指南</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E7%BD%91%E9%A1%B5%E6%90%AD%E5%BB%BA/" class="print-no-link">#网页搭建</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Hexo基于fluid模板的相册搭建</div>
      <div>https://l0223.github.io.git/2024/03/25/Hexo基于fluid主题的相册搭建/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>lynx</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2024年3月25日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2024/03/26/%E5%AF%BB%E6%89%BE%E4%BA%BA%E7%94%9F%E4%B9%8B%E8%B7%AF/" title="《如何找到想做的事》">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">《如何找到想做的事》</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2024/03/25/First%20post/" title="欢迎来到晓老板之家">
                        <span class="hidden-mobile">欢迎来到晓老板之家</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      

    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
